<template>
    <div class="views-shangpinxinxi-add">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加商品信息 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" label-width="140px" status-icon validate-on-rule-change>
                    <el-form-item label="商品编号" prop="shangpinbianhao" :rules="[{required:true, message:'请填写商品编号'}]">
                        <el-input type="text" placeholder="输入商品编号" style="width: 450px" v-model="form.shangpinbianhao" />
                    </el-form-item>

                    <el-form-item label="商品名称" prop="shangpinmingcheng" required :rules="[{required:true, message:'请填写商品名称'}]">
                        <el-input type="text" placeholder="输入商品名称" style="width: 450px" v-model="form.shangpinmingcheng" />
                    </el-form-item>

                    <el-form-item label="商品分类" prop="shangpinfenlei" required :rules="[{required:true, message:'请填写商品分类'}]">
                        <el-select v-model="form.shangpinfenlei"><e-select-option type="option" module="shangpinfenlei" value="id" label="fenleiming"></e-select-option></el-select>
                    </el-form-item>

                    <el-form-item label="商品图片" prop="shangpintupian" required :rules="[{required:true, message:'请填写商品图片'}]">
                        <e-upload-images v-model="form.shangpintupian" is-paste></e-upload-images>
                    </el-form-item>

                    <el-form-item label="商品附件" prop="shangpinfujian" required :rules="[{required:true, message:'请填写商品附件'}]">
                        <e-upload-file v-model="form.shangpinfujian"></e-upload-file>
                    </el-form-item>

                    <el-form-item label="价格" prop="jiage" required :rules="[{required:true, message:'请填写价格'}, {validator:rule.checkNumber, message:'输入一个有效数字'}]">
                        <el-input type="number" placeholder="输入价格" style="width: 450px" v-model.number="form.jiage" />
                    </el-form-item>

                    <el-form-item label="商品描述" prop="shangpinmiaoshu">
                        <e-editor v-model="form.shangpinmiaoshu" @getContent="getshangpinmiaoshuContent"></e-editor>
                    </el-form-item>

                    <el-form-item label="发布人" prop="faburen"> <el-input v-model="form.faburen" readonly style="width: 250px"></el-input> </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";
    import EEditor from "@/components/EEditor.vue";

    import { ref, reactive, computed } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useShangpinxinxiCreateForm, canShangpinxinxiInsert } from "@/module";
    import { extend } from "vue-design-plugin";

    const route = useRoute();
    const props = defineProps({
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
    });
    const { form } = useShangpinxinxiCreateForm();
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);
    var submit = () => {
        formModel.value.validate().then((res) => {
            if (loading.value) return;
            loading.value = true;
            canShangpinxinxiInsert(form).then(
                (res) => {
                    loading.value = false;
                    if (res.code == 0) {
                        emit("success", res.data);
                        if (props.isHouxu) {
                            ElMessage.success("添加成功");
                            formModel.value.resetFields();
                        }
                    } else {
                        ElMessageBox.alert(res.msg);
                    }
                },
                (err) => {
                    loading.value = false;
                    ElMessageBox.alert(err.message);
                }
            );
        });
    };

    const getshangpinmiaoshuContent = (v) => {
        form.shangpinmiaoshu = v;
    };
</script>

<style scoped lang="scss">
    .views-shangpinxinxi-add {
    }
</style>
